package cn.zqy.services;

import java.util.ArrayList;
import java.util.Arrays;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.w3c.dom.Node;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class Utils
{
	public static String createHtmlSelect(String name,String[] options,String selectedOption,boolean emptyAddition)
	{
		StringBuffer sb=new StringBuffer();
		sb.append("<select name=\"").append(name).append("\">");
		if(emptyAddition)
		{
			sb.append("<option value=\"\"></option>");
		}
		for(String option:options)
		{
			sb.append("<option ");
			if(option.equals(selectedOption))
			{
				sb.append(" selected ");
			}
			sb.append(" value=\"").append(option).append("\">").append(option).append("</option>");
		}
		sb.append("</select>");
		return sb.toString();
	}
	
	public static String createHtmlSelect(String name,ArrayList<String> options,ArrayList<String> values,String selectedValue)
	{
		StringBuffer sb=new StringBuffer();
		sb.append("<select name=\"").append(name).append("\" id=\"").append(name).append("\">");
		for(int i=0,iSize=options.size();i<iSize;i++)
		{
			String option=options.get(i);
			String value="";
			if(options.size()!=values.size())
			{
				value=option;
			}
			else
			{
				value=values.get(i);
			}
			
			sb.append("<option ");
			if(value.equals(selectedValue))
			{
				sb.append(" selected ");
			}
			sb.append(" value=\"").append(value).append("\">").append(option).append("</option>");
		}
		sb.append("</select>");
		return sb.toString();
	}
	
	
	public static boolean isAuthorizedEmail(String email)
	{
		String validEmails=ConfigService.getValue("ACL", "Authority");
		String[] emails=validEmails.split(",");
		
		if(Arrays.asList(emails).contains(email))
			return true;
		else 
			return false;
		
	}
	
	public static boolean isValidEmail(String email)
	{
		boolean value = true;
		if (email == null)
		{
			value = false;
		}
		try
		{
			new InternetAddress(email);
			String[] tokens=email.split("@");
			value=tokens.length==2&&tokens[0].length()>0&&tokens[1].length()>0;
		}
		catch (AddressException ex)
		{
			value = false;
		}

		return value;
	}
	
	public static String getXmlNodeValue(Node node) {
		String nodeValue = "";
		if (node == null) {
			return "";
		} else {
			nodeValue = node.getTextContent().trim();
			return nodeValue;
		}
	}
	
	public static boolean hasLogged()
	{
		boolean bool=false;
		UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
	    if (user != null) 
	    {
	    	bool=true;
	    }
		return bool;
	}
	
	public static boolean isAuthorized()
	{
		boolean bool=false;
		UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
	    if (user != null&&Utils.isAuthorizedEmail(user.getEmail())) 
	    {
	    	bool=true;
	    }
		return bool;
	}
}
